var ctx;
var cellInfoId = "";
var cellRegionId = "";
var cellBuildingId = "";
var cellUnitId = "";
var ownerHouseId = "";

function setCtx(ctx) {
	this.ctx = ctx;
	initListener();  //添加区域监听事件
}

//省市区级联
function initListener() {
	$("#province").change(function () {
		province = $('#province').val();
		if (province != '') {
			$.ajax({
		        type : "GET",
		        url : ctx + "/district/getDistrictInfoByParentId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	parentId : province
		        },
		        success : function(data) {
		        	if (data.success) {
		                var result = "<option value='" + "'>--城市--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.regionName + "'" + " value='" + item.id + "'>" + item.regionName + "</option>";
		                });
		                $("#city").html('');
		                $("#city").append(result);
		                $("#city").change();
		            }
		        }
			});
		} else {
			var result = "<option value='" + "'>--城市--</option>";
			$("#city").html('');
            $("#city").append(result);
            $("#city").change();
		}
    });
	
    $("#city").change(function () {
    	city = $('#city').val();
    	if (city != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/district/getDistrictInfoByParentId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	parentId : city
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--区--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.regionName + "'" + " value='" + item.id + "'>" + item.regionName + "</option>";
		                });
		                $("#district").html('');
		                $("#district").append(result);
		                $("#district").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--区--</option>";
			$("#district").html('');
            $("#district").append(result);
            $("#district").change();
    	}
    });
    
    
    $("#district").change(function () {
    	area = $('#district').val();
    	if (area != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellListByAreaId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	areaId : area
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--小区--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.cellName + "'" + " value='" + item.id + "'>" + item.cellName + "</option>";
		                });
		                $("#cellInfoId").html('');
		                $("#cellInfoId").append(result);
		                $("#cellInfoId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--小区--</option>";
			$("#cellInfoId").html('');
            $("#cellInfoId").append(result);
            $("#cellInfoId").change();
    	}
    });
    
    $("#cellInfoId").change(function () {
		cellInfoId = $('#cellInfoId').val();
		if (cellInfoId != '') {
			$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellRegionByCellId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	cellId : cellInfoId
		        },
		        success : function(data) {
		        	if (data.success) {
		                var result = "<option value='" + "'>--区域--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
		                });
		                $("#cellRegionId").html('');
		                $("#cellRegionId").append(result);
		                $("#cellRegionId").change();
		            }
		        }
			});
		} else {
			var result = "<option value='" + "'>--区域--</option>";
			$("#cellRegionId").html('');
            $("#cellRegionId").append(result);
            $("#cellRegionId").change();
		}
    });
	
    $("#cellRegionId").change(function () {
    	cellRegionId = $('#cellRegionId').val();
    	if (cellRegionId != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellBuildingByRegionId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	regionId : cellRegionId
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--楼栋--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.buildingName + "'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
		                });
		                $("#cellBuildingId").html('');
		                $("#cellBuildingId").append(result);
		                $("#cellBuildingId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--楼栋--</option>";
			$("#cellBuildingId").html('');
            $("#cellBuildingId").append(result);
            $("#cellBuildingId").change();
    	}
    });
    
    $("#cellBuildingId").change(function () {
    	cellBuildingId = $('#cellBuildingId').val();
    	if (cellBuildingId != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellUnitByBuildingId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	buildingId : cellBuildingId
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--单元--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
		                });
		                $("#cellUnitId").html('');
		                $("#cellUnitId").append(result);
		                $("#cellUnitId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--单元--</option>";
			$("#cellUnitId").html('');
            $("#cellUnitId").append(result);
            $("#cellUnitId").change();
    	}
    });
    
    $("#cellUnitId").change(function () {
    	cellUnitId = $('#cellUnitId').val();
    	
    	if (cellUnitId != ''){
    		$.ajax({
		        type : "GET",
		        url : ctx + "/ownerHouse/getCellHouseByUnitId",
		        cache : false,
		        dataType : 'json',
		        data : {
		        	unitId : cellUnitId
		        },
		        success : function(data) {
		        	if (data.success) {
		        		var result = "<option value='" + "'>--房屋--</option>";
		                $.each(data.obj, function(index, item) {
		                    result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
		                });
		                $("#cellHouseId").html('');
		                $("#cellHouseId").append(result);
		                $("#cellHouseId").change();
		            }
		        }
			});
    	} else {
    		var result = "<option value='" + "'>--房屋--</option>";
			$("#cellHouseId").html('');
            $("#cellHouseId").append(result);
            $("#cellHouseId").change();
    	}
    });
    
    
    $('#cellHouseId').change(function() {
    	if ($('#cellHouseId').val() != 0) {
    		ownerHouseId = $('#cellHouseId').val();
    	}
    });

}

//验证省市区不能为空
function checkProvinceName(){
	var province = $("#province").val();

	var district = $("#district").val();
	if(province == null || province == ""){
		layer.tips('请选择省!', '#province', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证城市名称不能为空
function checkCityName(){
	var city = $("#city").val();
	if(city == null || city == ""){
		layer.tips('请选择市!', '#city', {
			tips: 3,
			time:1000
		});
	return false;
	}else{
		return true;
	}
}

//验证地区名称不能为空
function checkAreaName(){
	var district = $("#district").val();
	if(district == null || district == ""){
		layer.tips('请选择地区!', '#district', {
			tips: 3,
			time:1000
		});
	return false;
	}else{
		return true;
	}
}
	


//验证房屋名称
function checkName(){
	var name = $("#cellHouseId").val();
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#cellHouseId").attr("placeholder")){
		name = "";
	}
	
	if(name == null || name == ""){
		layer.tips('房屋名称不能为空!', '#cellHouseId', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}


//验证小区不能为空
function checkCellInfo(){
	var infoId = $("#cellInfoId").val();
	
	if(infoId == null || infoId == ""){
		layer.tips('请选择小区!', '#cellInfoId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证区域不为空
function checkCellRegion(){
	var regionId = $("#cellRegionId").val();
	if(regionId == null || regionId == ""){
		layer.tips('请选择区域!', '#cellRegionId', {
			tips: 3,
		    time:1000
		});
	return false;
	}
	return true;
}

//验证楼栋不为空
function checkCellBuilding(){
	var buildingId = $("#cellBuildingId").val();
	
	if(buildingId == null || buildingId == ""){
		layer.tips('请选择楼栋!', '#cellBuildingId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证单元不为空
function checkCellUnit(){
	var cellUnitId = $("#cellUnitId").val();
	
	if(cellUnitId == null || cellUnitId == ""){
		layer.tips('请选择单元!', '#cellUnitId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//保存或新增方法
var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
function saveOrUpdateOwnerHouse() {
	
	var cellId = $("#cellInfoId").val();
	var houseId = ownerHouseId;
	var ownerId = $("#ownerId").val();
	var state = $('#state').val();
	var memo = $("#memo").val();
	
	
	var ownerHouseMessage = {};
	ownerHouseMessage.cellId = cellId;
	ownerHouseMessage.houseId = houseId;
	ownerHouseMessage.ownerId = ownerId;
	ownerHouseMessage.state = state;
	ownerHouseMessage.memo = memo;
	
	
	
	if (checkProvinceName() && checkCityName() && checkAreaName() && checkCellInfo() 
			&& checkCellRegion() && checkCellBuilding() && checkCellUnit() && checkName()) {
		
		var index1 = layer.load(1);
		$.post(ctx +"/ownerHouse/saveOrUpdateOwnerHouse", ownerHouseMessage, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					parent.window.location.href= ctx + "/ownerHouse/ownerHouseList";
					parent.layer.close(index); // 根据窗口索引关闭窗口
					
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					parent.layer.close(index); // 根据窗口索引关闭窗口
				});
			}
		});
	}
}

// 点击取消，返回上一页
function Back() {
	parent.layer.close(index);
}
